/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/25
 * Time: 下午1:11
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.Page;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import com.cake21.uitest.Constant.Constants;
import com.cake21.uitest.System.Core.Page;

public class CakeListPage extends Page {
    /*页面元素*/
    @FindBy(className = "gallery-list")
    private WebElement listDiv;
    @FindBy(css = ".goods-item")
    private List<WebElement> goodsList;
    @FindBy(id = "big_cart_dialog")
    private WebElement bigDialog;

    /*构造函数*/
    public CakeListPage() {

    }

    /*载入页面*/
    public void load() {
        driver.get(Constants.CakeListURL);
    }

    /*获得蛋糕列表数量*/
    public int getGoodsCount() {
        return goodsList.size();
    }

    /*点击进入随机的蛋糕详情页*/
    public CakePage goCakeDetail() {
        int total = goodsList.size();
        /*选个随机数*/
        int randomNumber = (int) (Math.random() * total);
        WebElement cake = goodsList.get(randomNumber);
        System.out.println("Random Number: " + randomNumber);
        cake.findElement(By.cssSelector(".action-view")).click();
        return new CakePage();
    }

    public Boolean getOneCakeToCart() {
        WebElement firstCake = goodsList.get(0);
        try {
            WebElement toCartBtn = firstCake.findElement(By.cssSelector(".action-addtocart"));
            toCartBtn.click();
            if (bigDialog.isDisplayed()) {
                WebElement btn = bigDialog.findElement(By.className("big_dialog_addcart"));
                btn.click();
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
